package com.junxonline.plat.controller.menu;

import com.junxonline.common.dao.model.Menu;
import com.junxonline.common.dao.vo.MenuVO;
import com.junxonline.common.dao.vo.QueryMenuVO;
import com.junxonline.common.dao.vo.ResultVO;
import com.junxonline.common.util.CommonUtils;
import com.junxonline.common.util.RedisUtils;
import com.junxonline.plat.controller.base.BaseController;
import com.junxonline.plat.exception.ErrorCodeEnum;
import com.junxonline.plat.service.menu.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JunX
 * @ClassName: MenuController
 * @Description: 菜单控制器
 */

@RestController
@RequestMapping(value = "/admin/menu")
public class MenuController extends BaseController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取导航菜单数据
     * @return
     */
    @RequestMapping(value = "/getNavMenu")
    public ResultVO getNavMenu() {
        Long userId = getUserId();
        return getReturnSuccess(menuService.getNavMenu(userId));
    }

    /**
     * 获取按钮数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getButton")
    public ResultVO getButton(@RequestBody MenuVO vo) {

        if (CommonUtils.isEmpty(vo.getMenuUrl())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Long userId = getUserId();
        return getReturnSuccess(menuService.getButton(vo.getMenuUrl(), userId));
    }

    /**
     * 获取菜单树数据
     * @return
     */
    @RequestMapping(value = "/getMenuTree")
    public ResultVO getMenuTree() {
        return getReturnSuccess(menuService.getMenuTree());
    }

    /**
     * 获取菜单数据
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getMenu")
    public ResultVO queryMenu(@RequestBody QueryMenuVO vo) {
        return getReturnSuccess(menuService.getMenu(vo));
    }

    /**
     * 获取菜单的所有子菜单
     * @param vo
     * @return
     */
    @RequestMapping(value = "/getMenuChildren")
    public ResultVO getMenuChildren(@RequestBody MenuVO vo) {

        if (CommonUtils.isEmpty(vo.getMenuId())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        return getReturnSuccess(menuService.getMenuChildren(vo.getMenuId()));
    }

    /**
     * 添加菜单
     * @param vo
     * @return
     */
    @RequestMapping(value = "/addMenu")
    public ResultVO addMenu(@RequestBody MenuVO vo) {

        if (CommonUtils.isEmpty(vo) || CommonUtils.isEmpty(vo.getMenuType()) || CommonUtils.isEmpty(vo.getMenuName()) ||
            CommonUtils.isEmpty(vo.getMenuUrl()) || CommonUtils.isEmpty(vo.getMenuPermission()) ||
            CommonUtils.isEmpty(vo.getMenuSort()) || CommonUtils.isEmpty(vo.getMenuStatus())) {
            return getReturnError(ErrorCodeEnum.PARAMS_IS_NULL);
        }

        Menu menu = menuService.addMenu(vo);
        if (CommonUtils.isEmpty(menu)) {
            return getReturnError(ErrorCodeEnum.SYSTEM_ERROR);
        }

        return getReturnSuccess();

    }

}
